package com.base.controller;

import com.base.po.voip.Payinfoes;
import com.base.po.voip.Rewards;
import com.base.po.voip.Subscribers;
import com.base.service.PayinfoesService;
import com.base.service.RewardsService;
import com.base.service.SubscribersService;
import com.base.service.WithdrawalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 顺通查询用户
 *
 * @author f
 *
 */
@Controller
@RequestMapping("/admin")
public class index_STSubscribers {
    @Autowired
    private SubscribersService subscribersService;
    @Autowired
    private RewardsService rewardsService;
    @Autowired
    private WithdrawalsService withdrawalsService;
    @Autowired
    private PayinfoesService payinfoesService;

    @RequestMapping("/index_STSubscribers")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        List<Subscribers> subList = subscribersService.selectByParentId(adminUserId);
        Subscribers sub = null;
        if (subList != null && subList.size() > 0){
            sub = subList.get(0);
        }

        float ig = 0f;
        List<Rewards> rewardsList = rewardsService.selectByPhase(sub.getParentid(), sub.getId());
        if (rewardsList != null && rewardsList.size() > 0){
            Rewards rewards = rewardsList.get(0);
            ig = rewards.getIntegral().floatValue();
        }
        float igw = 0f;
        List<Rewards> igwList = rewardsService.selectByWithdraw(sub.getParentid(), sub.getId());
        if (igwList != null && igwList.size() > 0){
            Rewards rewards = igwList.get(0);
            igw = rewards.getIntegral().floatValue();
        }
        float integral = rewardsService.countByIntegral(sub.getParentid(), sub.getId())
                + rewardsService.countByBonus(sub.getParentid(), sub.getId())
                + rewardsService.countByExtra(sub.getParentid(), sub.getId())
                - ig
                - (withdrawalsService.countByAlready(sub.getId(), 2, 1)/0.92f - igw);
        float all = rewardsService.countByIntegral(sub.getParentid(), sub.getId())
                + rewardsService.countByBonus(sub.getParentid(), sub.getId())
                + rewardsService.countByExtra(sub.getParentid(), sub.getId())
                - ig;
        sub.setIgrewardAll(integral);
        sub.setWithdrawalsAll((withdrawalsService.countByAlready(sub.getId(), 2, 1)/0.92f) - igw);
        sub.setMoneyAll(all);

        List<Payinfoes> payinfoesList = payinfoesService.selectByUserId(sub.getId());
        if (payinfoesList != null && payinfoesList.size() > 0){
            sub.setEndtime(payinfoesList.get(0).getPaytime());
        }else {
            sub.setEndtime(null);
        }

        modelAndView.setViewName("jsp/index_STSubscribers");
        request.setAttribute("subscribers", sub);
        request.setAttribute("username", sub.getMobile());
        return modelAndView;
    }

    @RequestMapping("/selectSTSubscribers")
    public ModelAndView selectSTSubscribers(HttpServletRequest request, String username) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();

        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        List<Subscribers> subList = subscribersService.selectBySub(username, adminUserId);
        Subscribers sub = null;
        if (subList != null && subList.size() > 0){
            sub = subList.get(0);
        }

        float ig = 0f;
        List<Rewards> rewardsList = rewardsService.selectByPhase(sub.getParentid(), sub.getId());
        if (rewardsList != null && rewardsList.size() > 0){
            Rewards rewards = rewardsList.get(0);
            ig = rewards.getIntegral().floatValue();
        }
        float igw = 0f;
        List<Rewards> igwList = rewardsService.selectByWithdraw(sub.getParentid(), sub.getId());
        if (igwList != null && igwList.size() > 0){
            Rewards rewards = igwList.get(0);
            igw = rewards.getIntegral().floatValue();
        }
        float integral = rewardsService.countByIntegral(sub.getParentid(), sub.getId())
                + rewardsService.countByBonus(sub.getParentid(), sub.getId())
                + rewardsService.countByExtra(sub.getParentid(), sub.getId())
                - ig
                - (withdrawalsService.countByAlready(sub.getId(), 2, 1)/0.92f - igw);
        float all = rewardsService.countByIntegral(sub.getParentid(), sub.getId())
                + rewardsService.countByBonus(sub.getParentid(), sub.getId())
                + rewardsService.countByExtra(sub.getParentid(), sub.getId())
                - ig;
        sub.setIgrewardAll(integral);
        sub.setWithdrawalsAll((withdrawalsService.countByAlready(sub.getId(), 2, 1)/0.92f) - igw);
        sub.setMoneyAll(all);

        List<Payinfoes> payinfoesList = payinfoesService.selectByUserId(sub.getId());
        if (payinfoesList != null && payinfoesList.size() > 0){
            sub.setEndtime(payinfoesList.get(0).getPaytime());
        }else {
            sub.setEndtime(null);
        }

        modelAndView.setViewName("jsp/index_STSubscribers");
        request.setAttribute("subscribers", sub);
        request.setAttribute("username", username);
        return modelAndView;
    }
}
